package com.ruoyi.planthopper.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.planthopper.domain.PlotInfo;
import com.ruoyi.planthopper.domain.UpdatePlot;
import com.ruoyi.planthopper.mapper.PlotMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @program: class by cxf
 * @author: AbrahamBill
 * @date: 2022/7/22
 **/
@Slf4j
@RestController
public class PlotController extends BaseController {
    @Autowired
    private PlotMapper plotMapper;
    /**
     * 获取所有地块信息
     */
    @GetMapping("/getAllPlotInfo")
    public AjaxResult getAllPlotInfo(){
        List<PlotInfo> lst = plotMapper.selectPlotList(null);
        List<String> ret = new ArrayList<>();
        for(PlotInfo el:lst){
            if(!ret.contains(el.getPlotName())){
                ret.add(el.getPlotName());
            }
        }
        Collections.reverse(ret);
        return AjaxResult.success(ret);
    }
    /**
     * 根据条件查询地块信息
     */
    @GetMapping("/getPlotByCondition")
    public AjaxResult getPlotByCondition(PlotInfo plotInfo){
//        QueryWrapper<PlotInfo> query = new QueryWrapper<>();
//        query.eq(StringUtils.isNotEmpty(plotInfo.getProvince()),"province",plotInfo.getProvince());
//        query.eq(StringUtils.isNotEmpty(plotInfo.getCity()),"city",plotInfo.getCity());
//        query.eq(StringUtils.isNotEmpty(plotInfo.getDistrict()),"district", plotInfo.getDistrict());
//        query.eq(StringUtils.isNotEmpty(plotInfo.getPlotName()),"plotName",plotInfo.getPlotName());
        List<PlotInfo> lst = plotMapper.selectPlotList(plotInfo);
        return AjaxResult.success(lst);
    }
    /**
     * 新增地块信息
     */
    @PostMapping("/insertNewPlot")
    public AjaxResult insertNewPlot(PlotInfo plotInfo){
        try{
            int result = plotMapper.insertPlot(plotInfo);
            return AjaxResult.success("新增成功！", plotInfo);
        }catch (Exception e){
            return AjaxResult.error("新增失败！", e);
        }
    }

    @PostMapping("/updatePlotNameById")
    public AjaxResult updatePlotName(UpdatePlot updatePlot){
        try{
            updatePlot.getPlotName();
            updatePlot.getId();
            if(plotMapper.updatePlotNameById(updatePlot)==1){
                return AjaxResult.success("修改成功！");
            }else{
                return AjaxResult.error("修改失败！地块不存在！");
            }
        }catch (Exception e){
            return AjaxResult.error("新地块名称或者地块id不能为空！");
        }
    }

}
